package com.android.takeaway.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.android.takeaway.Util;
import com.android.takeaway.R;
import com.android.takeaway.adapter.FoodAdapter;

import com.android.takeaway.bean.Food;

/**
 * 支付页面
 */
public class PayActivity extends AppCompatActivity implements View.OnClickListener {
    private List<Food> cartList;
    private BigDecimal totalMoney, distributionCost, allMoney;
    private TextView tv_back, tv_title, tv_totalMoney, tv_distributionCost,
            tv_totalCount, tv_payment;
    private RecyclerView recycleView_order;
    private FoodAdapter orderAdapter;
    private CardView cvAddress;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        initView();
    }

    private void initView() {
//        orderDao = new OrderDao(this);
        sp = getSharedPreferences("my_sp", MODE_PRIVATE);
        // 获取控件实例
        tv_back = findViewById(R.id.tv_back);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText("订单");
        cvAddress = findViewById(R.id.cv_address);

        tv_totalMoney = findViewById(R.id.tv_foodCost);
        tv_distributionCost = findViewById(R.id.tv_distributionCost);
        tv_totalCount = findViewById(R.id.tv_totalCount);
        tv_payment = findViewById(R.id.tv_payment);
        // 设置监听器
        tv_payment.setOnClickListener(this);
        tv_back.setOnClickListener(this);
        // 定义和加载适配器
        recycleView_order = findViewById(R.id.recycleView_order);
        recycleView_order.setLayoutManager(new LinearLayoutManager(this));
        orderAdapter = new FoodAdapter(this);
        initData();
        recycleView_order.setAdapter(orderAdapter);
        orderAdapter.setData(cartList);
    }

    // 初始化数据源
    private void initData() {
        Intent intent = getIntent();
        cartList = (List<Food>) intent.getSerializableExtra("cartList");
        totalMoney = (BigDecimal) intent.getSerializableExtra("totalMoney");
        distributionCost = (BigDecimal) intent.getSerializableExtra("distributionCost");

        tv_totalMoney.setText("￥" + totalMoney);  //商品小计
        tv_distributionCost.setText("￥" + distributionCost); //运费
        allMoney = totalMoney.add(distributionCost);
        tv_totalCount.setText("￥" + allMoney); //总订单
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_payment:
                payGoods();
                break;
        }
    }

    private void payGoods() {
        // 加载payment.xml布局
        LayoutInflater inflater = LayoutInflater.from(this);
        View paymentView = inflater.inflate(R.layout.payment, null);

// 创建并显示对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(paymentView);
        final AlertDialog dialog = builder.create();
        dialog.show();

// 延迟5秒关闭对话框
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
                // 提交订单
                commitOrder();
            }
        }, 5000);
    }

    private void commitOrder() {
        // 当前用户的账号
        String acc = sp.getString("acc", "");
        // 订单的编号（当前日期转换的字符串）
        SimpleDateFormat numberDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = numberDateFormat.format(new Date());
        // 获取订单时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        // 获取购买店铺
        String shop = getIntent().getStringExtra("shopName");
        // 获取订单价格
        String price = "小计￥" + totalMoney.toString() + " 运费￥" + distributionCost.toString()
                + " 总计￥" + allMoney.toString();
        StringBuilder content = new StringBuilder();
        // 获取订单内容
        for (Food food : cartList) {
            content.append(food.getFoodName());
            content.append("x");
            content.append(food.getCount());
            content.append(" ");
        }

        Util.showToast(this, "支付成功，已提交订单");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {

                }
                break;
            default:
                break;
        }
    }
}